<?php

App::uses('AppController', 'Controller');

/**
 * DocumentosOrdenes Controller
 *
 * @property DocumentosOrdene $DocumentosOrdene
 */
class DocumentosOrdenesController extends AppController
{

    public $uses = array('DocumentosOrdene', 'Ordene', 'OrdenesTag', 'Tag', 'Categoria', 'Documento', 'Cambiordene', 'CambiordenesTag');
    public $layout = 'epcm';

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->DocumentosOrdene->recursive = 0;
        $this->set('documentosOrdenes', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        $this->DocumentosOrdene->id = $id;
        if (!$this->DocumentosOrdene->exists())
        {
            throw new NotFoundException(__('Invalid documentos ordene'));
        }
        $this->set('documentosOrdene', $this->DocumentosOrdene->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post'))
        {
            $this->DocumentosOrdene->create();
            if ($this->DocumentosOrdene->save($this->request->data))
            {
                $this->Session->setFlash(__('The documentos ordene has been saved'));
                $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The documentos ordene could not be saved. Please, try again.'));
            }
        }
        $documentos = $this->DocumentosOrdene->Documento->find('list');
        $ordenes = $this->DocumentosOrdene->Ordene->find('list');
        $this->set(compact('documentos', 'ordenes'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function editar($id = null)
    {
        //debug($this->data);exit;
        $cdoc=$this->DocumentosOrdene->find('first', array('recursive'=>-1,
        'conditions'=>array('id'=>$id)
        ));
  
        $var=$cdoc['DocumentosOrdene']['ordene_id'];
                
        $this->DocumentosOrdene->id = $id;

        $dcd = $this->Documento->find('list', array(
            'fields' => array('nombre')
                ));
                     
        if (!$this->DocumentosOrdene->exists())
        {
            throw new NotFoundException(__('Invalid documentos ordene'));
        }
        if ($this->request->is('post') || $this->request->is('put'))
        {
            if(empty($this->data['DocumentosOrdene']['original'])){
                $this->request->data['DocumentosOrdene']['original']=0;
            }
            if ($this->DocumentosOrdene->save($this->request->data))
            {
                $this->Session->setFlash(__('The documentos ordene has been saved'));
                $this->redirect(array('controller' => 'ordenes', 'action' => 'editadoc', $var));
            } else
            {
                $this->Session->setFlash(__('The documentos ordene could not be saved. Please, try again.'));
            }
        } else
        {
            $this->request->data = $this->DocumentosOrdene->read(null, $id);
        }
        //$dcd = $this->DocumentosOrdene->Documento->find('list');
        $ordenes = $this->DocumentosOrdene->Ordene->find('list');
        $this->set(compact('dcd', 'orden'));
    }

    function eliminar($id=null){
        
         $cdocu=$this->DocumentosOrdene->find('first', array('recursive'=>-1,
        'conditions'=>array('id'=>$id)
        ));
        $var=$cdocu['DocumentosOrdene']['ordene_id'];
     //  debug($cdocu);exit;  
        
        $this->DocumentosOrdene->id=$id;
        $this->data=$this->DocumentosOrdene->read();
        if(!$id){
            $this->Session->setFlash('No existe el Documento a eliminar');
            $this->redirect(array('action' =>'index'));
        }
        else{
            if($this->DocumentosOrdene->delete($id)){
                $this->Session->setFlash('Se elimino el Documento '.$this->data['DocumentosOrdene']['documento_id']);
                $this->redirect(array('controller'=>'Ordenes', 'action' => 'editadoc', $var));
            }
            else{
                $this->Session->setFlash('Error al eliminar');
            }
        }
    } 


    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        if (!$this->request->is('post'))
        {
            throw new MethodNotAllowedException();
        }
        $this->DocumentosOrdene->id = $id;
        if (!$this->DocumentosOrdene->exists())
        {
            throw new NotFoundException(__('Invalid documentos ordene'));
        }
        if ($this->DocumentosOrdene->delete())
        {
            $this->Session->setFlash(__('Documentos ordene deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Documentos ordene was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
